Dictionary is an unordered set of key-value pairs where the keys are unique. Dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples.
In [3]:
dict1 = {'id': 1, 'name':'John Doe', 'email':'john.doe@example.org','salary':14000.00}
dict1
Out[3]:
In [4]:
dict1['name']
Out[4]:
In [5]:
dict1['email']='john.doe@example.com'
In [6]:
dict1
Out[6]:
In [24]:
type(dict1)
Out[24]:
In [25]:
str(dict1)
Out[25]:
In [7]:
list(dict1.keys())
Out[7]:
In [8]:
list(dict1.values())
Out[8]:
In [9]:
sorted(list(dict1.keys()))
Out[9]:
In [43]:
del(list)
In [14]:
dict2 = dict([('id',2),('name','Jack Jill'),('salary',15500)])
dict2
Out[14]:
In [23]:
dict(id=3, salary=25000)
In [16]:
dict = [dict1,dict2]
dict
Out[16]:
In [17]:
dict[1]['name']
Out[17]:
In [18]:
{x: x**2 for x in range(10) if x%2 == 0}
Out[18]:
In [29]:
people = {'name':'John','email':'john@example.com'}
for k,v in people.items():
print(k, v)
In [31]:
for key in people:
print(people[key])
In [32]:
for value in people.values():
print(value)
In [33]:
for key in people.keys():
print(key)
In [47]:
__builtins__.list
countries = ["USA","England","France","India","Japan"]
capitals = ["Washington","London","Paris","New Delhi","Tokyo"]
countries_dict = dict(zip(countries,capitals))
print(countries_dict)
In [ ]: